/*
 * Copyright 2023 LINE Corporation
 *
 * LINE Corporation licenses this file to you under the Apache License,
 * version 2.0 (the "License"); you may not use this file except in compliance
 * with the License. You may obtain a copy of the License at:
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations
 * under the License.
 */

package com.linecorp.bot.codegen.pebble.function;

import io.pebbletemplates.pebble.template.EvaluationContext;
import io.pebbletemplates.pebble.template.PebbleTemplate;
import org.openapitools.codegen.CodegenModel;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * We have a method, CodegenModel.requiredVars, which unfortunately includes the discriminator.
 * One could think of implementing a separate filter to handle this, however, we ran into an
 * issue where {% if loop.last %},{% endif %} isn't operating as expected. To address this,
 * we've integrated a control mechanism in the Java part of the code.
 */
public class RequiredVarsFunction implements io.pebbletemplates.pebble.extension.Function {
    @Override
    public List<String> getArgumentNames() {
        return Collections.singletonList("model");
    }

    @Override
    public Object execute(Map<String, Object> args, PebbleTemplate self, EvaluationContext context, int lineNumber) {
        CodegenModel model = (CodegenModel) args.get("model");
        return model.allVars.stream()
                .filter(var -> var.required)
                .collect(Collectors.toList());
    }
}
